package com.agent.action.controller;

import com.agent.action.client.DifyWorkClient;
import com.agent.action.entity.QuestionEntity;
import com.agent.action.interfaces.WorkFlowClient;
import com.agent.action.param.*;
import com.agent.action.service.GenerateQuestionService;
import com.agent.action.vo.QuestionVO;
import com.agent.commons.Result.Result;
import com.agent.commons.errorcode.ControllerErrorCodeConstants;
import com.agent.commons.exception.BaseException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.SignalType;

/**
 *dify工作流控制器
 */
@Slf4j
@RestController
@RequestMapping("/dify")
public class GenerateQuestionsController {
    @Resource
    private GenerateQuestionService generateQuestionService;

    /**
     * 流式生成单个题目
     * @param questionEntity
     * @return Flux<QuestionVO>
     */
    @PostMapping(value = "/generate/question",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<QuestionVO> generateQuestion(@RequestBody QuestionParam questionEntity) {
        return generateQuestionService.streamQuestion(questionEntity);
    }

    /**
     * 流式生成练习
     * @param testParam
     * @return Flux<QuestionVO>
     */
    @PostMapping(value = "/generate/test",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<QuestionVO> generateTest(@RequestBody TestParam testParam){
        log.error("generateTest:{}",testParam);
        if(testParam.getTotal()!=testParam.getChoice()+testParam.getFill()+testParam.getEssay()) {
            throw new BaseException(ControllerErrorCodeConstants.NUMBER_BREAK_EDGE);
        }
        return generateQuestionService.streamTest(testParam);
    }

    /**
     * 流式生成markdown
     * @param markDownParam
     * @return
     */
    @PostMapping(value = "/generate/markdown",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<QuestionVO> generateMarkDown(@RequestBody MarkDownParam markDownParam){
        log.info("generateMarkDown:{}",markDownParam);
        return generateQuestionService.streamMarkDown(markDownParam);
    }

    /**
     * 暂停流式响应
     * @return Result<Boolean>
     */
    @PostMapping("/pause/{userId}/{entity}/{taskId}")
    public Result<Boolean> pause(@PathVariable String userId,@PathVariable String entity,@PathVariable String taskId){
        FlowPauseParam pauseParam = new FlowPauseParam();
        pauseParam.setTaskId(taskId);
        pauseParam.setUserId(userId);
        pauseParam.setEntity(entity);
        generateQuestionService.pause(pauseParam);
        return Result.success();
    }

}
